package com.blockmeta.bbs.immodule.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.businesslibrary.util.RouterActivityHelper;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.adapter.FansAdapter;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.net.api.GetWatchApi;
import com.blockmeta.bbs.immodule.net.pojo.Fans;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author airthor
 * @date 2019/5/15
 * 我关注的人
 */
public class FansActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener{
    @BindView(R2.id.rcv_fans_list)
    RecyclerView mRcvFansList;

    private List<Fans> mFriendsEntries = new ArrayList<>();
    private FansAdapter mMembersAdapter;
    private long mUserId;

    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_fans,null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        mUserId = getIntent().getLongExtra(Params.USER_TAG, 0);
        setTitleText("粉丝");
        initView();
        network();
    }

    private void network() {
        RetrofitFactory.getRetrofit().create(GetWatchApi.class)
                .getWatch(mUserId)
                .compose(this.<BaseImResponse<List<Fans>>>bindToLifecycle())
                .compose(RxUtil.<List<Fans>>commonResult())
                .compose(RxUtil.<List<Fans>>rxSchedulerHelper())
                .subscribe(new CommonSubscriber<List<Fans>>(mContext) {
                    @Override
                    public void onNext(List<Fans> list) {
                        mFriendsEntries.addAll(list);
                        mMembersAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void initView() {
        mRcvFansList.setLayoutManager(new LinearLayoutManager(mContext));
        mMembersAdapter = new FansAdapter(R.layout.item_contacts_friends, mFriendsEntries, false);
        mMembersAdapter.bindToRecyclerView(mRcvFansList);
        mMembersAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Fans item = mFriendsEntries.get(position);
        RouterActivityHelper.openUserCenter(mContext, String.valueOf(item.getFansId()));
    }
}
